/**
 * Created by jackliu on 15/12/6.
 */

import SJGPage = require('./../kernel/cont/SJGPage');
import ObjManager = require('./../kernel/cont/ObjManager');
import TextObj = require('./../kernel/obj/TextObj');
import TextExtObj = require('./../kernel/obj/TextExtObj');
import GroupObj = require('./../kernel/obj/GroupObj');
import SJGReplaceGroup = require('./../kernel/replace/SJGReplaceGroup');
import SJGReplaceItemImage = require('./../kernel/replace/SJGReplaceItemImage');
import SJGReplaceItemText = require('./../kernel/replace/SJGReplaceItemText');
import SJGSubGroupText = require('./../kernel/replace/SJGSubGroupText');
import SJGTypeSetItemBase = require('./SJGTypeSetItemBase');
import SJGTypeSetBoxUtil2 = require('./SJGTypeSetBoxUtil2');
import ObjBase = require('./../kernel/obj/ObjBase');
import CommonUtil = require('./../../util/CommonUtil');
import ObjPositionAlign = require('./../kernel/base/ObjPositionAlign');
import GCJ_CLASS_TYPE= require('./../common/GCJ_CLASS_TYPE');
import ParaContour = require('./../common/graph/ParaContour');
var async  = require( 'async'); // 同步控制
export = SJGTypeSetTextGroup2;

class SJGTypeSetTextGroup2 extends  SJGTypeSetItemBase{

    // 根据坐标和宽高排版对象，需要被重载
    // 参数：p页面
    // 参数：group标题组
    // 参数：i可替换对象的下标
    // 参数：pos对象的宽高
    public typesetObject(p:SJGPage,group: SJGReplaceGroup,i:number, pos:ObjPositionAlign){
        var objMgr:ObjManager = p.getGlobalData().objManager();
        var groupObj:GroupObj = <GroupObj>objMgr.getObject(group.id);// 原版面中的对象
        var replaceItemText2:SJGSubGroupText = <SJGSubGroupText>group.value[i];
        var G:GroupObj = null;

        if(i<groupObj.children.length){
            // 修改旧对象GroupObj G以及其文字子对象TextObj T
            G =<GroupObj> groupObj.children[i];
        }else{
            // 创建新对象,深拷贝最后一个子GroupObj
            // 得到GroupObj G以及其文字子对象TextObj T
            G =<GroupObj> CommonUtil.deepCopy(groupObj.children[groupObj.children.length-1],p.getGlobalData().ver,p.getGlobalData());
        }

        // 处理旧文字TextObj
        var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_TextObj);
        for(var k=0;k<arr.length;k++){
            var T:TextObj =<TextObj> objMgr.getObject(arr[k]);
            if(!T) continue;

            for(var n in replaceItemText2.value){// 目前最多只会有2个子对象（小标题＋正文）
                var repTxt = replaceItemText2.value[n];
                if (repTxt.tt == T.tt){// 文字类型一致（小标题/正文）
                    // 设置T的te和pc
                    T.te = repTxt.value;
                    T.pc = ParaContour.fromFontServerData(repTxt.value, JSON.parse(repTxt.originContour));
                    // 重新排版文字
                    T.refresh();
                    break;
                }
            }
        }

        // 处理新文字TextExtObj 20160623
        for(var n in replaceItemText2.value){// 目前最多只会有2个子对象（小标题＋正文）
            var repTxt = replaceItemText2.value[n];

            var obj:ObjBase =objMgr.getObject(repTxt.id);
            if(!obj)    continue;
            if(obj.ct != GCJ_CLASS_TYPE.CLASS_TYPE_TextExtObj)
                continue;
            var T:TextExtObj = <TextExtObj>obj;
            if(!T)  continue;

            // TextType 文字类型: 0普通正文／1小标题
            //if(repTxt.tt == 0){
                // 设置T的te和pc
                T.te = repTxt.value;
                T.pc = ParaContour.fromFontServerData(repTxt.value, JSON.parse(repTxt.originContour));

                // 重新排版文字
                // T.refresh();// 这里可以 refresh 可以注释，可以在 SJGTypeSetItemBase.do 中调用
            //}else if(repTxt.tt == 1){
            //    if(T.title){
            //        var title:TextExtObj=<TextExtObj>T.title;
            //        title.te = repTxt.value;
            //        title.pc = ParaContour.fromFontServerData(repTxt.value, JSON.parse(repTxt.originContour));
            //        // 重新排版文字
            //        //title.refresh();// 这里可以 refresh 可以注释，可以在 SJGTypeSetItemBase.do 中调用
            //    }
            //}
        }



        // 对于圆形扩展，不进行缩放，移动位置即可
        if(group.advance.extend_method == SJGReplaceGroup.CIRCLE){
            G.align = pos;
            var ap = pos.toPos();
            G.pos.copy(ap);


            return;
        }
        // 调整编组G和内部子对象的大小
        //var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_TextObj,true);
        //for(var k in arr){
        //    var other:ObjBase =  objMgr.getObject(arr[k]);
        //    other.constrain.scale = 0; // 将可变对象之外的所有对象约束设置为固定
        //}

        var ret:Array<number> = SJGTypeSetBoxUtil2.calcRatioXY(groupObj.children.length, group.value.length,
            group.advance.extend_method,group.advance.limit);
        G.expand(1, ret[0],  1,      false); // 向右扩展
        G.expand(2, 1,       ret[1], false);// 向下扩展

        console.log("******* expand:"+ret[0]+","+ret[1]+",calcRatioXY:"
            +groupObj.children.length+','+ group.value.length+','+group.advance.extend_method+','+group.advance.limit);

    }

    // 排版后到调整：例如文字组需要增减字号达到填满效果
    // 参数：p页面
    // 参数：group标题组
    // 参数：callback 为async的回调函数，例如：asyncCallback(null, 'one');
    public after(p:SJGPage,group: SJGReplaceGroup ,callback) {
        var objMgr:ObjManager = p.getGlobalData().objManager();
        var groupObj:GroupObj = <GroupObj>objMgr.getObject(group.id);// 原版面中的对象
        var G:GroupObj = null;

        // 获取最大字数的文字对象、最大字数、到父Group的距离
        var maxTextToBottom:number = 0;
        var maxTextLen:number= 0;
        var maxText:TextObj=null;
        for(var i in groupObj.children){
            G =<GroupObj> groupObj.children[i];
            if(!G)  return;


            var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_TextObj);
            var T:TextObj =<TextObj> objMgr.getObject(arr[0]);
            if(!T)  return;

            if(T.te.length>maxTextLen){
                maxTextToBottom = T.pos.h;
                maxText = T;
                maxTextLen = T.te.length;
            }
        }

        // 缩放字号
        if(maxTextLen>0 && maxTextToBottom>0 && maxText.te.length>0){
            var rw:number = Math.sqrt((maxTextToBottom*maxText.pos.w)/maxTextLen);
            var ratio:number = (rw-maxText.cs-maxText.ls)/maxText.fs;
            console.log("======>TextGroup ratio is: ",maxTextToBottom," ", ratio)
            if(ratio>0){
                for(var i in groupObj.children){
                    G =<GroupObj> groupObj.children[i];
                    if(!G)  return;

                    var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_TextObj);
                    var T:TextObj =<TextObj> objMgr.getObject(arr[0]);
                    if(!T)  return;
                    // 缩放字号、字间距、行间距
                    T.fs *= ratio;
                    T.cs *= ratio;
                    T.ls *= ratio;
                    // 缩放每个字的轮廓
                    T.pc.adjustPosition(ratio,ratio,2);

                    // 重新排版
                    T.refresh();
                }
            }
        }

        // 回调
        callback(null, null);
    }
}
